package com.idlecode.view;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.idlecode.panel.CodeGenerationPanel;
import com.idlecode.panel.DataSourcesPanel;
import com.idlecode.panel.FieldTypeMappingPanel;
import org.eclipse.jface.action.*;
import org.eclipse.ui.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;

import jakarta.inject.Inject;

public class IdleCodeView extends ViewPart {

	public static final String ID = "com.idlecode.view.IdleCodeView";

	@Inject
	IWorkbench workbench;

	private Action codeGenerationAction;
	private Action dataSourcesAction;
	private Action fieldTypeMappingAction;

	private StackLayout stackLayout;
	private Composite parentComposite;
	private Composite codeGenerationPage;
	private Composite dataSourcesPage;
	private Composite fieldTypeMappingPage;

	@Override
	public void createPartControl(Composite parent) {
		parentComposite = new Composite(parent, SWT.NONE);
		stackLayout = new StackLayout();
		parentComposite.setLayout(stackLayout);

		createPages(parentComposite);

		makeActions();
		contributeToActionBars();
	}

	private void createPages(Composite parent) {
		codeGenerationPage = new CodeGenerationPanel(parent, SWT.NONE);

		dataSourcesPage = new DataSourcesPanel(parent, SWT.NONE);

		fieldTypeMappingPage = new FieldTypeMappingPanel(parent, SWT.NONE);

		stackLayout.topControl = codeGenerationPage;
		parent.layout();
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(codeGenerationAction);
		manager.add(new Separator());
		manager.add(dataSourcesAction);
		manager.add(new Separator());
		manager.add(fieldTypeMappingAction);
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(codeGenerationAction);
		manager.add(dataSourcesAction);
		manager.add(fieldTypeMappingAction);
	}

	private void makeActions() {
		codeGenerationAction = new Action() {
			public void run() {
				stackLayout.topControl = codeGenerationPage;
				parentComposite.layout();
			}
		};
		codeGenerationAction.setText("代码生成");
		codeGenerationAction.setToolTipText("代码生成");
		// 设置图标
		codeGenerationAction.setImageDescriptor(
				AbstractUIPlugin.imageDescriptorFromPlugin("com.idlecode", "icons/codeGeneration2.png"));
//		codeGenerationAction.setImageDescriptor(
//				PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));

		dataSourcesAction = new Action() {
			public void run() {
				stackLayout.topControl = dataSourcesPage;
				parentComposite.layout();
			}
		};
		dataSourcesAction.setText("数据源");
		dataSourcesAction.setToolTipText("数据源");
		// 设置图标
		dataSourcesAction.setImageDescriptor(
				AbstractUIPlugin.imageDescriptorFromPlugin("com.idlecode", "icons/dataSources.png"));
//		dataSourcesAction
//				.setImageDescriptor(workbench.getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));

		fieldTypeMappingAction = new Action() {
			public void run() {
				stackLayout.topControl = fieldTypeMappingPage;
				parentComposite.layout();
			}
		};
		fieldTypeMappingAction.setText("字段类型映射");
		fieldTypeMappingAction.setToolTipText("字段类型映射");
		// 设置图标
		fieldTypeMappingAction.setImageDescriptor(
				AbstractUIPlugin.imageDescriptorFromPlugin("com.idlecode", "icons/fieldTypeMapping.png"));
//		fieldTypeMappingAction
//				.setImageDescriptor(workbench.getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
	}

	@Override
	public void setFocus() {
		// viewer.getControl().setFocus();
	}
}
